<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import type {Graph} from "@/interface";
import * as echarts from "echarts";
import {useGetGraphDataApi} from "@/api";


const graph = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const barData = ref<Graph>({
  data:[],
  category:[],
  edges:[]
})
const init = async () => {
  const {data:res} = await useGetGraphDataApi()
  barData.value = res
  myChart.value = markRaw(echarts.init(graph.value));
  draw();
}
const draw = () => {
  option.value = {
    title: {
      text: '房屋与房型关系图'
    },
    tooltip: {},
    series: [{
      type: 'graph',
      layout: 'force',
      draggable: true,
      data: barData.value.data,
      categories: barData.value.category,
      edges: barData.value.edges,
      roam: true,
      label: {
        position: 'right'
      },
      force: {
        repulsion: 50
      }
    }]
  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="graph" class="graph">
  </div>
</template>

<style scoped>
.graph{
  width: 100%;
  height: 100%;
}
</style>